package com.example.smart.base.dialog

import android.content.Context
import android.view.Gravity
import android.view.View
import com.example.lib.extend.dip2px
import com.example.lib.extend.getDeviceWidth
import com.example.lib.extend.setMargin
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.extend.visi
import com.example.smart.R
import com.example.smart.base.dialog.base.BaseDialog
import com.example.smart.databinding.DialogTipsBinding

class TipsDialog(context: Context) : BaseDialog<DialogTipsBinding>(width = ((getDeviceWidth(context) * 0.77).toInt()),
    dialogContext = context,
    gravity = Gravity.CENTER) {
    private var mTitle = ""
    private var mMsg = ""
    private var mOkText = context.getString(com.example.lib.R.string.sure)
    private var mOkTextColor = context.getColor(com.example.lib.R.color.white)
    private var mCancelTextColor = context.getColor(com.example.lib.R.color.blue_4279ff)
    private var mCancelText = context.getString(com.example.lib.R.string.cancel)
    private var success: (com.example.smart.base.dialog.TipsDialog) -> Unit = {}
    private var cancel: (com.example.smart.base.dialog.TipsDialog) -> Unit = {}
    var msgMaxLines = 4

    override fun setLayoutRes() = R.layout.dialog_tips

    override fun afterViewCreate(view: View?) {
        banCancel()
        binding.DialogExitTipsCancel.setOnDoubleClickListener {
            dismiss()
            cancel.invoke(this@TipsDialog)
        }

        binding.DialogExitTipsDone.setOnDoubleClickListener {
            success.invoke(this@TipsDialog)
        }
    }

    fun setDoneListener(success: (com.example.smart.base.dialog.TipsDialog) -> Unit): com.example.smart.base.dialog.TipsDialog {
        this.success = success
        return this
    }

    fun setCancelListener(cancel: (com.example.smart.base.dialog.TipsDialog) -> Unit): com.example.smart.base.dialog.TipsDialog {
        this.cancel = cancel
        return this
    }

    fun setOkText(str: String): com.example.smart.base.dialog.TipsDialog {
        this.mOkText = str
        return this
    }

    fun setOkTextColor(color: Int): com.example.smart.base.dialog.TipsDialog {
        this.mOkTextColor = context.getColor(color)
        return this
    }

    fun setCancelTextColor(color: Int): com.example.smart.base.dialog.TipsDialog {
        this.mCancelTextColor = context.getColor(color)
        return this
    }

    fun setCancelText(str: String): com.example.smart.base.dialog.TipsDialog {
        this.mCancelText = str
        return this
    }

    fun setMsg(msg: String): com.example.smart.base.dialog.TipsDialog {
        this.mMsg = msg
        return this
    }

    fun setTitle(title: String): com.example.smart.base.dialog.TipsDialog {
        this.mTitle = title
        return this
    }

    override fun show() {
        super.show()
        if (mTitle.isNotEmpty()) {
            binding.DialogTipsTitle.visi()
            binding.DialogTipsTitle.text = mTitle
            binding.DialogTipsMsg.setMargin(top = context.dip2px(10f))
        } else {
            binding.DialogTipsMsg.setMargin(top = context.dip2px(37f))
        }
        binding.DialogTipsMsg.text = mMsg
        binding.DialogTipsMsg.maxLines = msgMaxLines
        binding.DialogExitTipsCancel.text = mCancelText
        binding.DialogExitTipsCancel.setTextColor(mCancelTextColor)
        binding.DialogExitTipsDone.text = mOkText
        binding.DialogExitTipsDone.setTextColor(mOkTextColor)
    }
}